#include "Util.h"

int* Util::copyArray( int *src, int size ) {
	int *copy = new int[size];
	for (int i = 0; i < size; i++)
		copy[i] = src[i];

	return copy;
}

double* Util::copyArray( double *src, int size ) {
	double *copy = new double[size];
	for (int i = 0; i < size; i++)
		copy[i] = src[i];

	return copy;
}

char* Util::copyArray( const char *src, int size ) {
	char *copy = new char[size];
	for (int i = 0; i < size; i++)
		copy[i] = src[i];

	return copy;
}

int Util::random( int min, int max, double *w ) {
	if (w == NULL) {
		return min + rand() * (max - min);
	}
	
	int n = max - min + 1;
	double x = rand();
	double sw = 0;

	for (int i = 0; i < n; i++) {
		sw += w[i];
		if (x < sw)
			return min + i;
	}
}

int Util::random( int *v, int n, double *w ) {
	return v[random(0, n-1, w)];
}

double Util::random( double min, double max ) {
	return min + rand() * (max - min);
}

int Util::map( int i, int j, int maxj ) {
	return i * maxj + j;
}